// pages/dayDetail/dayDetail.js
import {
  hgetStorage,
  hshowToast,
  hnavigateBack
} from "../../static/htools.wx";
import {
  hrequestGet,
  hrequestPut
} from "../../static/hunit.wx";
import api from '../../assets/api'
const {
  getDeclareDayDetail,
  updateDeclareDay
} = api
import {
  hMouthDay,
  hyearMouthDay
} from '../../static/handleTimes'
import WxValidate from '../../static/WxValidate'
Page({
  data: {
    id: '',
    details: {},
    forecastNumber: '',
  },
  onLoad(options) {
    this.initValidate() //验证规则函数
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.getDetail(options.id)
    }
  },
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  initValidate() {
    const rules = {
      forecastNumber: {
        required: true,
        number: true
      }
    }
    const messages = {
      forecastNumber: {
        required: '请输入预估电量',
        number: '请输入数字'
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  // 表单验证并提交申报
  async formSubmit(e) {
    const params = e.detail.value
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      console.log(this.WxValidate.errorList)
      this.showModal(error)
      return false
    }
    let _dataJson = {
      actualNumber: this.data.details.declareDayDto.actualNumber == null ? '' : this.data.details.declareDayDto.actualNumber,
      adjustmentNumber: this.data.details.declareDayDto.adjustmentNumber == null ? '' : this.data.details.declareDayDto.adjustmentNumber,
      correctNumber: this.data.details.declareDayDto.correctNumber == null ? '' : this.data.details.declareDayDto.correctNumber,
      declareDay: this.data.details.declareDayDto.declareDay,
      deptId: hgetStorage('deptId'),
      forecastNumber: this.data.forecastNumber,
      id: this.data.id,
      status: 1
    }
    let _res = await hrequestPut(updateDeclareDay, _dataJson)
    if (_res.statusCode === 204) {
      hshowToast('提交成功')
      let pages = getCurrentPages(); //定义页面 
      let prevPage = pages[pages.length - 2]; //上一个页面
      if (prevPage) {
        //上一页存在则给上一个data赋值。则把title传过去
        prevPage.setData({
          isDayUpdate: true,
          dayPage:0,
          isShowDayMore:true
        })
      }
      hnavigateBack(1)
    } else {
      hshowToast('数据过长请重新输入提交')
    }
  },
  // 日前数据
  async getDetail(id) {
    let _res = await hrequestGet(getDeclareDayDetail + '/' + id)
    console.log(_res)
    _res.data.declareDayDto.mouthDay = hMouthDay(_res.data.declareDayDto.declareDay);
    _res.data.declareDayDto.yearMouthDay = hyearMouthDay(_res.data.declareDayDto.createTime)
    this.setData({
      details: _res.data
    })
  },
  //双向绑定
  forecastNum(e) {
    this.setData({
      forecastNumber: e.detail.value
    })
  },
  inputBlur(e) {
    let _number = e.currentTarget.dataset.number;
    if (e.detail.value === "") {
      this.setData({
        forecastNumber: _number,
        'details.declareDayDto.forecastNumber': _number
      })
    }
  }
})